from django.shortcuts import render

# Create your views here.
from django.views import View
from .models import *
from rest_framework.views import APIView
from myapp import myserializers
from rest_framework.response import Response


class RegisterView(APIView):
    def post(self, request):
        user_ser = myserializers.UserSerializer(data=request.data)
        if user_ser.is_valid():
            user_ser.save()
            return Response({'code': 200, 'data': '创建成功'})
        else:
            return Response({'code': 500, 'data': '创建失败'})


class LoginView(APIView):
    def post(self, request):
        data = request.data
        print(data)
        user_ser = User.objects.filter(username=data['username'], password=data['password']).first()
        if user_ser:
            return Response({'code': 200, 'data': '登陆成功'})
        else:
            return Response({'code': 500, 'data': '登陆失败,账号不存在'})
